/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, WebView} from 'react-native';
import Home from './src/index';

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

// ts语法，可以暂时去掉
type Props = {};
export default class App extends Component<Props> {
  test() {
    alert('test');
  }
  error(err) {
    alert(err)
  }
  onLoad() {
    alert('onLoad')
  }
  onLoadEnd = () => {
    alert('onLoadEnd')
  }
  onLoadStart() {
    alert('onLoadStart')
  }
  onMessage(e) {
    console.log(e.nativeEvent.data);
  }
  tap() {
    // 为啥在load完成时wb还没在ref中？
    // 因为在render时确实wb还没放进去,放到componentDidMount
    this.refs.wb.postMessage('22222')
  }
  render() {
    return (
      <View style={{marginTop: 100, width: '100%', height: '100%'}}>
        <Text onPress={this.tap.bind(this)}>btn</Text>
        <WebView
          ref={'wb'}
          source={{uri: 'http://192.168.199.227:2222/web'}}
          // automaticallyAdjustContentInsets={false}
          style={{marginTop: 40, width: '100%', height: '100%'}}
          // // injectJavaScript={this.test()}
          mediaPlaybackRequiresUserAction={false}
          onError={this.error}
          onLoad={this.onLoad}
          onLoadStart={this.onLoadStart}
          onLoadEnd={this.onLoadEnd}
          onMessage={e => this.onMessage(e)}
          />
      </View>
    );
  }
  componentDidMount() {
    console.log(this.refs.wb)
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});
